import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;

public class EBook {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        Connection connection = dataSource.getConnection();

//        String sql = "INSERT INTO borrow_info VALUES (NULL, 10, 3, '2019-09-25 17:50:00', '2019-10-25 17:50:00')";
//        PreparedStatement statement = connection.prepareStatement(sql);
//        statement.executeUpdate();

//        String sql = "select * from borrow_info where borrow_info.book_id in (select id from book where category_id = 3)";
//        PreparedStatement statement = connection.prepareStatement(sql);
//        ResultSet resultSet = statement.executeQuery();
//
//        while (resultSet.next()) {
//            int id = resultSet.getInt("id");
//            int bookId = resultSet.getInt("book_id");
//            int studentId = resultSet.getInt("student_id");
//            Timestamp startTime = resultSet.getTimestamp("start_time");
//            Timestamp endTime = resultSet.getTimestamp("end_time");
//            System.out.println("id: " + id + " book_id: " + bookId + " student_id: " + studentId + " start_time: " + startTime + " end_time: " + endTime);
//        }
//
//        resultSet.close();

//        String sql = "update book set price = 61.20 where name = '深入理解Java虚拟机'";
//        PreparedStatement statement = connection.prepareStatement(sql);
//        statement.executeUpdate();

        String sql = "delete A from borrow_info A, (select max(id) from borrow_info) B where A.id = B.id";
        PreparedStatement statement = connection.prepareStatement(sql);
        int n = statement.executeUpdate();
        System.out.println(n);

        statement.close();
        connection.close();
    }
}
